/// <reference path="../typings/globals/jquery/index.d.ts" />

'use strict'
var TreeEvent = require('./js/event.js')
var Configure = require('./js/configure.js')

function createSelectDiv(visible, addr) {
    let comboDiv = document.createElement("div")
    let select = document.createElement('select')

    let option = document.createElement('option')
    option.appendChild(document.createTextNode('192.168.2.131(舒特亮)'))
    option.setAttribute('value', '192.168.2.131')
    option.selected = (addr === '192.168.2.131')
    select.appendChild(option)

    option = document.createElement('option')
    option.appendChild(document.createTextNode('192.168.2.143(朱宇)'))
    option.setAttribute('value', '192.168.2.143')
    option.selected = (addr === '192.168.2.143')
    select.appendChild(option)

    option = document.createElement('option')
    option.appendChild(document.createTextNode('192.168.2.216(朱少雄)'))
    option.setAttribute('value', '192.168.2.216')
    option.selected = (addr === '192.168.2.216')
    select.appendChild(option)

    option = document.createElement('option')
    option.appendChild(document.createTextNode('192.168.2.98(吕伟)'))
    option.setAttribute('value', '192.168.2.98')
    option.selected = (addr === '192.168.2.98')
    select.appendChild(option)

    option = document.createElement('option')
    option.appendChild(document.createTextNode('192.168.2.2(付慧瑾)'))
    option.setAttribute('value', '192.168.2.2')
    option.selected = (addr === '192.168.2.2')
    select.appendChild(option)

    option = document.createElement('option')
    option.appendChild(document.createTextNode('192.168.2.138(李亚琴)'))
    option.setAttribute('value', '192.168.2.138')
    option.selected = (addr === '192.168.2.138')
    select.appendChild(option)

    option = document.createElement('option')
    option.appendChild(document.createTextNode('192.168.2.156(马海东)'))
    option.setAttribute('value', '192.168.2.156')
    option.selected = (addr === '192.168.2.156')
    select.appendChild(option)

    comboDiv.appendChild(document.createTextNode('分配给：'))
    comboDiv.appendChild(select)

    let label = document.createElement('lable')
    label.appendChild(document.createTextNode('close'))
    label.classList.add('close')
    comboDiv.appendChild(label)

    if (!visible) {
        comboDiv.setAttribute('style', 'visibility: hidden')
    }

    return comboDiv
}

function initVersionConfigure(elements) {
    elements.children().each(function(index, element) {
        let level = Number(element.getAttribute('level'))
        element.style.paddingLeft = 20 * level + 'px'
    })
}

function fillSlaveAddr() {
    let $slaveAddr = $('#slave-addr')
    let dict = {
        "07_x86": "192.168.2.98",
        "08_x86": "192.168.2.98",
        "09_x86": "192.168.2.2",
        "09_x64": "192.168.2.2",
        "10_x86": "192.168.2.216",
        "10_x64": "192.168.2.216",
        "11_x86": "192.168.2.143",
        "11_x64": "192.168.2.143",
        "12_x86": "192.168.2.138",
        "12_x64": "192.168.2.138",
        "13_x86": "192.168.2.131",
        "13_x64": "192.168.2.131",
        "14_x86": "192.168.2.156",
        "14_x64": "192.168.2.156",
    }
    $('#cad-version-config').children().each(function(index, element) {
        let id = element.getElementsByTagName('input')[0].id
        $slaveAddr.append(createSelectDiv(element.hasAttribute("Out"), dict[id])) // 向slaveAddr添加隐藏选项   
    })
}

window.onresize = function() {}

window.onload = function() {
    // 初始化缩进
    initVersionConfigure($('#cad-version-config'))
    initVersionConfigure($('#plugin-target'))
    // 加载下拉框
    fillSlaveAddr()
    Configure.load()

    // 为复选框添加点击事件
    $('#cad-version-config input[type=checkbox]').bind(TreeEvent.checkboxClick())
    $('#plugin-target input[type=checkbox]').bind(TreeEvent.checkboxClick())
    $('#plugin-target>div').bind(TreeEvent.divClick())
    $('#tool-button>button').bind(TreeEvent.buttonClick())
}
